home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Tracker 4.0 / automaton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  5.7 KB  |  237 lines  |  [TEXT/KAHL]

  1. /* automaton.c */
  2.  
  3. /* $Id: automaton.c,v 4.0 1994/01/11 17:42:13 espie Exp espie $
  4.  * $Log: automaton.c,v $
  5.  * Revision 4.0  1994/01/11  17:42:13  espie
  6.  * Abstracted IO calls.
  7.  *
  8.  * Revision 1.5  1994/01/09  23:24:37  Espie
  9.  * Last bug fix.
  10.  *
  11.  * Revision 1.4  1994/01/09  17:36:22  Espie
  12.  * Generalized open.c.
  13.  *
  14.  * Revision 1.3  1994/01/05  14:54:09  Espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 1.2  1993/12/28  13:54:44  Espie
  18.  * Use display_pattern.
  19.  *
  20.  * Revision 1.1  1993/12/26  00:55:53  Espie
  21.  * Initial revision
  22.  *
  23.  * Revision 3.16  1993/12/04  16:12:50  espie
  24.  * *** empty log message ***
  25.  *
  26.  * Revision 3.15  1993/11/17  15:31:16  espie
  27.  * *** empty log message ***
  28.  *
  29.  * Revision 3.13  1993/07/18  10:39:44  espie
  30.  * Fixed up repeat code, should work better now.
  31.  *
  32.  * Revision 3.12  1993/07/17  22:23:41  espie
  33.  * Fixed bug with bad loops.
  34.  *
  35.  * Revision 3.9  1993/05/09  14:06:03  espie
  36.  * Modified the way set_speed works.
  37.  *
  38.  * Revision 3.8  1993/01/16  17:00:27  espie
  39.  * Corrected stupid bug (run_in_fg)
  40.  *
  41.  * Revision 3.7  1993/01/15  14:00:28  espie
  42.  * Added bg/fg test.
  43.  *
  44.  * Revision 3.6  1992/11/27  10:29:00  espie
  45.  * General cleanup
  46.  *
  47.  * Revision 3.5  1992/11/24  10:51:19  espie
  48.  * un#ifdef'ed showseq code.
  49.  *
  50.  * Revision 3.3  1992/11/22  17:20:01  espie
  51.  * Simplified delay_pattern.
  52.  *
  53.  * Revision 3.2  1992/11/20  14:53:32  espie
  54.  * Added finetune.
  55.  *
  56.  * Revision 3.1  1992/11/19  20:44:47  espie
  57.  * Protracker commands.
  58.  *
  59.  * Revision 3.0  1992/11/18  16:08:05  espie
  60.  * New release.
  61.  *
  62.  * Revision 2.16  1992/11/17  17:15:37  espie
  63.  * New output for new interface
  64.  * Modified repeat logic: now works irregardless of repeat points.
  65.  * start
  66.  *
  67.  * Revision 2.8  1992/07/14  14:23:41  espie
  68.  * Changed fine speed command and comments.
  69.  * Added two level of fault tolerancy.
  70.  */
  71.      
  72.  
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76.      
  77. #include "defs.h"
  78. #include "song.h"
  79. #include "channel.h"
  80. #include "extern.h"
  81.      
  82. ID("$Id: automaton.c,v 4.0 1994/01/11 17:42:13 espie Exp espie $")
  83.      
  84.  
  85. LOCAL void clear_repeats(a, from, upto)
  86. struct automaton *a;
  87. int from, upto;
  88.    {
  89.    int i;
  90.  
  91.    for (i = from; i <= upto; i++)
  92.       a->gonethrough[i] = FALSE;
  93.    }
  94.  
  95. LOCAL void reset_repeats(a)
  96. struct automaton *a;
  97.    {
  98.    clear_repeats(a, 0, a->info->length);
  99.    a->gonethrough[a->info->length] = TRUE;
  100.    }
  101.  
  102. /* updates the pattern to play in the automaton.
  103.  * Checks that the pattern actually exists.
  104.  * Checks for repetitions as well.
  105.  */
  106. LOCAL void set_pattern(a)
  107. struct automaton *a;
  108.    {
  109.    int p;
  110.  
  111.  
  112.    if (a->pattern_num >= a->info->length)
  113.       {
  114.       error = UNRECOVERABLE;
  115.       return;
  116.       }
  117.  
  118.    if (a->gonethrough[a->pattern_num])
  119.       {
  120.       error = ENDED;
  121.       reset_repeats(a);
  122.       }
  123.    else
  124.       a->gonethrough[a->pattern_num] = TRUE;
  125.  
  126.    display_pattern(a->pattern_num, a->info->length);
  127.  
  128.       /* there is a level of indirection in the format,
  129.        * i.e., patterns can be repeated.
  130.        */
  131.    p = a->info->patnumber[a->pattern_num];
  132.    if (p >= a->info->maxpat)
  133.       {
  134.       error = UNRECOVERABLE;
  135.       return;
  136.       }
  137.    a->pattern = a->info->pblocks + p;
  138.    }
  139.  
  140. /* initialize all the fields of the automaton necessary
  141.  * to play a given song.
  142.  */
  143. void init_automaton(a, song, start)
  144. struct automaton *a;
  145. struct song *song;
  146. int start;
  147.    {
  148.    a->info = &song->info;
  149.    a->pattern_num = start;    /* first pattern */
  150.  
  151.    a->loop_note_num = 0;
  152.    a->loop_counter = 0;
  153.  
  154.    reset_repeats(a);
  155.  
  156.    a->note_num = 0;           /* first note in pattern */
  157.    a->counter = 0;            /* counter for the effect tempo */
  158.    a->speed = NORMAL_SPEED;   /* this is the default effect tempo */
  159.    a->finespeed = NORMAL_FINESPEED;    
  160.                               /* this is the fine speed (100%=NORMAL_FINESPEED) */
  161.    a->do_stuff = DO_NOTHING;   
  162.                               /* some effects affect the automaton,
  163.                                * we keep them here.
  164.                                */
  165.  
  166.    error = NONE;              /* Maybe we should not reset errors at
  167.                                * this point.
  168.                                */
  169.    set_pattern(a);
  170.    }
  171.  
  172. /* Gets to the next pattern, and displays stuff */
  173. LOCAL void advance_pattern(a)
  174. struct automaton *a;
  175.    {
  176.    if (++a->pattern_num >= a->info->length)
  177.       a->pattern_num = 0;
  178.    set_pattern(a);
  179.    a->note_num = 0;
  180.    }
  181.  
  182.         
  183.  
  184. /* process all the stuff which we need to advance in the song,
  185.  * including set_speed, set_skip, set_fastskip, and set_loop.
  186.  */
  187. void next_tick(a)
  188. struct automaton *a;
  189.    {
  190.       /* there are three classes of speed changes:
  191.        * 0 does nothing. (should stop)
  192.        * <32 is the effect speed (resets the fine speed).
  193.        * >=32 changes the finespeed, default 125
  194.        */
  195.    if ((a->do_stuff & SET_SPEED) && (a->do_stuff & SET_FINESPEED))
  196.       {
  197.       a->speed = a->new_speed;
  198.       a->finespeed = a->new_finespeed;
  199.       }
  200.    else if (a->do_stuff & SET_FINESPEED)
  201.       {
  202.       a->finespeed = a->new_finespeed;
  203.       }
  204.    else if (a->do_stuff & SET_SPEED)
  205.       {
  206.       a->speed = a->new_speed;
  207.       a->finespeed = NORMAL_FINESPEED;
  208.       }
  209.  
  210.    if (++a->counter >= a->speed)
  211.       {
  212.       a->counter = 0;
  213.          /* loop: may change note in pattern right away */
  214.       if ((a->do_stuff & JUMP_PATTERN) && --a->loop_counter > 0)
  215.          a->note_num = a->loop_note_num;
  216.       else if (a->do_stuff & SET_FASTSKIP)
  217.          {
  218.          a->pattern_num = a->new_pattern;
  219.          set_pattern(a);
  220.          a->note_num = 0;
  221.          }
  222.       else if (a->do_stuff & SET_SKIP)
  223.          {
  224.          advance_pattern(a);
  225.          a->note_num = a->new_note;
  226.          }
  227.       else
  228.          {
  229.          if (++a->note_num >= BLOCK_LENGTH)
  230.             advance_pattern(a);
  231.          }
  232.       a->do_stuff = DO_NOTHING;
  233.       }
  234.    }
  235.  
  236.  
  237.